package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.crm.entity.GwfeT;
import cn.e4j.modular.crm.service.impl.GwfeTServiceImpl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 实际费用核销
 * @author mengyb
 * @Date 2019/10/25
 */
@Controller
@RequestMapping("/crm/gwft400")
public class Gwft400Controller extends BaseController {

    @Autowired
    private GwfeTServiceImpl gwfeTService;

    /**
     * 跳转回款页面
     */
    @RequiresPermissions("crm:gwft400:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwft400.html";
    }


    @RequiresPermissions("crm:gwft400:init")
    @RequestMapping("/getList")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {

        Dto inDto = dtoWapper.getDto();
        Page<Object> page = dtoWapper.getPage();
        List<Dto> resultList = this.gwfeTService.getList(inDto,page);
        return new PageReData<>(page,resultList);
    }

    /**
     * 新增页面,获取默认值
     */
    @RequestMapping("/getAddDef")
    @RequiresPermissions("crm:gwft400:init")
    @ResponseBody
    public ResponseData getAddDef() {
        Dto outDto= Dtos.newDto();
        outDto.put("GWFEDOCNO","FYHX"+ DateUtils.formatDate(new Date(),"yyMMSSSS"));
        outDto.put("GWFEDOCDATE",DateUtils.getDate("yyyy-MM-dd"));
        return ResponseData.success(outDto);
    }

    /**
     * 获取汇率
     */
    @ResponseBody
    @RequiresPermissions("crm:gwft400:init")
    @RequestMapping("/getRate")
    public ResponseData getRate(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = this.gwfeTService.getRate(inDto);
        return ResponseData.success(outDto);
    }

    /**
     * 编辑页面,获取回显单身数据
     */
    @RequestMapping("/getSingleData")
    @RequiresPermissions("crm:gwft400:init")
    @ResponseBody
    public ResponseData getGweoData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto=dtoWapper.getDto();
        List<Dto> resultDto = this.gwfeTService.listGwfe(inDto);
        return ResponseData.success(resultDto);
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwft400:add")
    @ResponseBody
    public ResponseData addData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwfeT> gwfeTList = inDto.getReqList(GwfeT.class);
        this.gwfeTService.add(gwfeTList);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwft400:edit")
    @ResponseBody
    public ResponseData edit(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwfeT> gwfeTList = inDto.getReqList(GwfeT.class);
        this.gwfeTService.edit(gwfeTList);
        return SUCCESS_TIP;
    }

    /**
     * 删除数据
     */
    @RequestMapping("/delRow")
    @RequiresPermissions("crm:gwft400:init")
    @ResponseBody
    public ResponseData delRow(GwfeT gwfeT) {
        this.gwfeTService.delRow(gwfeT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delRows")
    @RequiresPermissions("crm:gwft400:init")
    @ResponseBody
    public ResponseData delRows(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwfeT> gwfeTList = inDto.getReqList(GwfeT.class);
        this.gwfeTService.delRows(gwfeTList);
        return SUCCESS_TIP;
    }

    /**
     * 批量审核数据
     */
    @RequestMapping("/check")
    @RequiresPermissions("crm:gwft400:init")
    @ResponseBody
    public ResponseData check(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwfeT> gwfeTList = inDto.getReqList(GwfeT.class);
        Dto dto=this.gwfeTService.check(gwfeTList);
        if(dto.getAppCode()==400){
            return ERROR_TIP;
        }
        return SUCCESS_TIP;
    }

    /**
     * 批量取消审核数据
     */
    @RequestMapping("/unCheck")
    @RequiresPermissions("crm:gwft400:init")
    @ResponseBody
    public ResponseData unCheck(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwfeT> gwfeTList = inDto.getReqList(GwfeT.class);
        Dto dto=this.gwfeTService.unCheck(gwfeTList);
        if(dto.getAppCode()==400){
            return ERROR_TIP;
        }
        return SUCCESS_TIP;
    }

    /**
     * 费用耗用核查
     */
    @RequestMapping("/getCostCheckData")
    @RequiresPermissions("crm:gwft400:init")
    @ResponseBody
    public ResponseData getCostCheckData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwfeT> gwfe003List = JSONObject.parseArray(jsonObject.getString("_xdata"), GwfeT.class);
        List<GwfeT> gwfe004List = JSONObject.parseArray(jsonObject.getString("_ydata"), GwfeT.class);
        Dto dto=this.gwfeTService.getCostCheckData(gwfe003List, gwfe004List, (String) jsonObject.get("GWEF001"), (String)jsonObject.get("GWEF002"), (String)jsonObject.get("areas"));
        return ResponseData.success(dto);
    }

}
